﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using uz.control.CustomControl;

namespace uz.control.Entity
{
    public class Entity :TNB_Label
    {
        public Entity()
        {
            ToolBar.AddCommand(CMD_EDIT_STR, "编辑（F2）");
            ToolBar.AddCommand(CMD_DEL_STR, "删除（F3）");
            ToolBar.AddCommand(CMD_ADD_STR, "添加实体（F4）");
            ToolBar.AddCommand(CMD_ADD_CHILD_STR, "添加字段（F5）");

            this.ID = "Entity";
            this.Name = "实体";
        }

        public static AutoComplateWindows AutoComplateWindows = new AutoComplateWindows();

        public override void DrawNodeText(PaintEventArgs e, Brush brush, Rectangle Bounds)
        {

            SizeF sf = SizeF.Empty;
            string str = (Index+1).ToString().PadLeft((Parent.Nodes.Count-1).ToString().Length,'0');
            sf = GetDrawTextSizeF(e.Graphics, UzTreeView.Font, str);
            float xOffset = Bounds.Left + HandleAreaWidth + GetIndentWidth() + 0;
            float yOffset = Bounds.Top +  5;
            e.Graphics.DrawString(str, UzTreeView.Font, brush, xOffset,yOffset);

            xOffset += sf.Width + 2;
            str = this.ID;
            sf = GetDrawTextSizeF(e.Graphics, UzTreeView.Font, str);
            e.Graphics.DrawString(str, UzTreeView.Font, brush, xOffset, yOffset);

            Brush myBrush = null;

            xOffset += sf.Width + 2;
            myBrush = new SolidBrush(Color.Blue);
            str = "（" + this.Name + "）";
            sf = GetDrawTextSizeF(e.Graphics, UzTreeView.Font, str);
            e.Graphics.DrawString(str, UzTreeView.Font, myBrush, xOffset, yOffset);
            myBrush.Dispose();

            xOffset += sf.Width + 2;
            myBrush = new SolidBrush(Color.Green);
            str = "（" + (Nodes.Count) + "）";
            sf = GetDrawTextSizeF(e.Graphics, UzTreeView.Font, str);
            e.Graphics.DrawString(str, UzTreeView.Font, myBrush, xOffset, yOffset);
            myBrush.Dispose();
        }

        public override bool EndEdit(UzTextBox txt)
        {
            string strData = txt.Text.Trim();
            if (strData == "")
            {
                ShowMessage("必须输入实体的ID和名称！");
                txt.SelectAll();
                return false;
            }
            string[] strAry = strData.Split(' ');
            string id = "";
            string name = "";

            foreach (string str in strAry)
            {
                if (id == "")
                {
                    id += str.Trim();
                }
                else
                {
                    name += str.Trim();
                }
            }

            if (id == "" || name == "")
            {
                ShowMessage("格式不正确！");
                txt.SelectAll();
                return false;
            }

            this.ID = id;
            this.Name = name;

            Word word = null;

            word = new Word();
            word.ID = this.ID;
            word.DisplayText = this.ID + " " + this.Name;
            word.Text = word.DisplayText;
            WordIndexManager.EntityWordIndex.AddWord(word);

            return true;
        }

        public override void BeginEdit(UzTextBox txt)
        {
            txt.Text = ID + " " + Name;
            txt.SelectAll();

            txt.AutoComplateWindows = AutoComplateWindows;
            AutoComplateWindows.OpenAutoComplate(txt, WordIndexManager.EntityWordIndex);
        }

        private void ShowMessage(string msg)
        {
            string str = "";
            str += "  输入格式如下：\n";
            str += "  实体ID+空格+实体名称 （例：people 人）";


            str = msg + "\n" + str;

            MessageBox.Show(str);
        }

        public override void Delete()
        {
            DialogResult rst = MessageBox.Show(
                "确定要删除这个实体吗？",
                "操作确认",
                MessageBoxButtons.YesNo, 
                MessageBoxIcon.Information, 
                MessageBoxDefaultButton.Button2
            );

            UzTreeNode node = GetNextVisibleNode();
            if (node == null)
            {
                node = GetPrevVisibleNode();
            }

            if (rst == DialogResult.Yes)
            {
                Parent.Nodes.RemoveAt(Index);
            }
            else
            {
                return;
            }

            if (node != null)
            {
                UzTreeView.SelectedNode = node;
            }
        }

        public override UzTreeNode AddChild()
        {
            EntityField field = new EntityField();
            Nodes.Add(field);
            field.IsEdit = true;

            if (IsExpanded == false)
            {
                Expand();
            }

            return field;
        }
    }
}
